package com.yuhang.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yuhang.entity.HyPet;
import com.yuhang.entity.HyUser;
import com.yuhang.service.HyPetService;
import com.yuhang.service.HyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotEmpty;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yuhang
 * @since 2021-04-24
 */
@RestController
@RequestMapping("/hy-pet")
public class HyPetController {

    @Value("${imgPath}")//获取springboot配置文件中的变量,把配置文件中的变量赋值给该属性
    private String path;
    @Autowired
    private HyPetService hyPetService;

    @RequestMapping("/showPet")
    public Map<String, Object> showPet(@RequestParam(value = "page",defaultValue = "1")int pageNum,
                                       @RequestParam(value = "limit",defaultValue = "5")int pageSize,
                                       @RequestParam(value = "likename",defaultValue = "")String likename){
        IPage<HyPet> page = hyPetService.selectByPage(pageNum, pageSize,likename);
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",page.getTotal());
        map.put("data",page.getRecords());
        return map;
    }

    @RequestMapping("/showUserPet")
    public Map<String, Object> showUserPet(HttpSession session,
                                           @RequestParam(value = "page", defaultValue = "1") int pageNum,
                                           @RequestParam(value = "limit", defaultValue = "5") int pageSize) {
        HyUser hyUser = (HyUser)session.getAttribute("hyUser");
        IPage<HyPet> page = hyPetService.selectByPage(pageNum, pageSize, hyUser.getId());
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", page.getTotal());
        map.put("data", page.getRecords());
        return map;
    }
    @RequestMapping("/addPet")
    public Map<String,Object> addPet(HyPet hyPet){
        hyPetService.save(hyPet);
        Map<String, Object> map = new HashMap<>();
        map.put("code",0);
        return map;
    }
    @RequestMapping("/addUserPet")
    public Map<String,Object> addUserPet(HttpSession session,HyPet hyPet){
        HyUser hyUser = (HyUser)session.getAttribute("hyUser");
        hyPet.setUserId(hyUser.getId());
        hyPet.setUserName(hyUser.getName());
        hyPetService.save(hyPet);
        Map<String, Object> map = new HashMap<>();
        map.put("code",0);
        return map;
    }
    @RequestMapping("/deletePet")
    public Map<String,Object> deletePet(@NotEmpty Integer id){
        hyPetService.removeById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code",0);
        return map;
    }
    @RequestMapping("/updatePet")
    public Map<String,Object> updatePet(HyPet hyPet){
        hyPetService.updateById(hyPet);
        Map<String, Object> map = new HashMap<>();
        map.put("code",0);
        return map;
    }
    @RequestMapping("/uploadImage")
    public Map<String,Object> uploadHeadImg(MultipartFile file) throws IOException {//收参使用file
        file.transferTo(new File(path,file.getOriginalFilename()));
        Map<String,Object> map=new HashMap<>();
        map.put("code",0);
        map.put("msg",file.getOriginalFilename());
        return map;
    }

}
